﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.BaseDoc.Model;
using Yipusoft.Account.Model;

public partial class Task_BaseDoc_DocumentFilePaste : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }

    private void Bind()
    {

        BindDepartmentList();
        SetDate();
        dptStartYear.SelectedValue = DateTime.Now.Year.ToString();
        dptStartMonth.SelectedValue = DateTime.Now.Month.ToString();
        BindParent();

    }

    void BindDepartmentList()
    {
        string Departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Departmentid.Substring(0, 4) == "1300")
        {
            trDepartment.Visible = false;
        }
        else if (Departmentid.Substring(4, 1) == "0")//支队
        {
            trDepartment.Visible = true;
            string where = " Departmentid like '13000" + Departmentid.Substring(5, 1) + "%' ";
            List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList(where);
            cblDepartment.DataSource = departments;
            cblDepartment.DataBind();

        } if (Departmentid.Substring(4, 1) != "0")//大中队
        {
            trDepartment.Visible = true;
            string where = " Departmentid like '" + Departmentid.Substring(0, 4) + "0%' and right(DepartmentID,3)<>'000' ";
            List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList(where);
            cblDepartment.DataSource = departments;
            cblDepartment.DataBind();
        }

    }

    private void BindParent()
    {
        dptDir.Items.Clear();

        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        List<Yipusoft.BaseDoc.Model.DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(" IsFolder='True' And DepartmentID='" + DepartmentID + "' And ParentID='" + Guid.Empty + "'and year(startDate)=" + dptStartYear.SelectedValue + " and month(startDate)=" + dptStartMonth.SelectedValue + " Order by OrderID ");
        foreach (DocumentFileInfo file in list)
        {
            dptDir.Items.Add(new ListItem("".PadLeft(2 * file.Level.Value, '　') + file.OldFileName, file.ID.ToString()));
            OrderItem(file.ID);



        }
        dptDir.Items.Insert(0, new ListItem("顶级分类", "00"));
    }

    private void SetDate()
    {
        for (int i = 2009; i <= 2020; i++)
        {
            dptStartYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
            //dptEndYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            dptStartMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
            //dptEndMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
    }
    private void OrderItem(Guid ID)
    {
        List<Yipusoft.BaseDoc.Model.DocumentFileInfo> list = Yipusoft.BaseDoc.BLL.FileManager.GetList(" IsFolder='True'  And ParentID='" + ID + "' Order by OrderID ");
        if (list != null)
        {
            foreach (DocumentFileInfo info in list)
            {
                dptDir.Items.Add(new ListItem("".PadLeft(2 * info.Level.Value, '　') + info.OldFileName, info.ID.ToString()));
                OrderItem(info.ID);
            }
        }

    }
    protected void ibtnPaste_Click(object sender, ImageClickEventArgs e)
    {
        if (!cblParent.Checked && !cbSelf.Checked)
        {
            lblPrompt.Text = "提示：请选择粘贴级别！";
            return;
        }
        if (Request["ID"] == null || Request["Flag"] == null)
            return;
        lblPrompt.Text = "";
        string[] strArray = Request["ID"].Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
        if (cbSelf.Checked)
        {

            if (Request["Flag"].Equals("Cut"))
            {
                Guid parentID = Guid.Empty;
                if (dptDir.SelectedValue != "00")
                {
                    parentID = new Guid(dptDir.SelectedValue);
                }
                foreach (string id in strArray)
                {
                    if (new Guid(id) != Guid.Empty)
                    {
                        DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(id));
                        info.ParentID = parentID;
                        info.EndDate = DateTime.MaxValue;
                        if (dptDir.SelectedValue == "00")
                        {
                            info.Level = 0;
                        }
                        else
                        {
                            info.Level = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(dptDir.SelectedValue)).Level + 1;
                        }

                        Yipusoft.BaseDoc.BLL.FileManager.UpdateDocumentFileInfo(info);
                    }
                }

            }
            if (Request["Flag"].Equals("Copy"))
            {
                Guid parentID = Guid.Empty;
                if (dptDir.SelectedValue != "00")
                {
                    parentID = new Guid(dptDir.SelectedValue);
                }


                foreach (string id in strArray)
                {
                    if (new Guid(id) != Guid.Empty)
                    {
                        DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(id));
                        info.ID = Guid.NewGuid();
                        info.ParentID = parentID;
                        if (dptDir.SelectedValue == "00")
                        {
                            info.Level = 0;
                        }
                        else
                        {
                            info.Level = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(dptDir.SelectedValue)).Level + 1;
                        }
                        info.EndDate = DateTime.MaxValue;
                        Yipusoft.BaseDoc.BLL.FileManager.AddFile(info);
                    }
                }
            }
        }

        if (cblParent.Checked)
        {

            //上报上级单位
            for (int i = 0; i < cblDepartment.Items.Count; i++)
            {
                if (cblDepartment.Items[i].Selected)
                {
                    foreach (string id in strArray)
                    {
                        if (new Guid(id) != Guid.Empty)
                        {
                            DocumentFileInfo info = Yipusoft.BaseDoc.BLL.FileManager.GetFile(new Guid(id));
                            info.ID = Guid.NewGuid();
                            info.ParentID = Guid.Empty;
                            info.Level = 0;
                            info.DepartmentID = cblDepartment.Items[i].Value;
                            info.CreateDate = DateTime.Now;
                            info.CreateUser = User.Identity.Name;
                            info.StartDate = DateTime.Now;
                            info.EndDate = DateTime.MaxValue;
                            Yipusoft.BaseDoc.BLL.FileManager.AddFile(info);
                        }
                    }
                }
            }
        }
        lblPrompt.Text = "文件已粘贴成功";
    }

    protected void dptStartYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindParent();
    }
    protected void dptStartMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindParent();
    }
    protected void cblParent_CheckedChanged(object sender, EventArgs e)
    {
        if (!cblParent.Checked)
        {
            for (int i = 0; i < cblDepartment.Items.Count; i++)
            {
                cblDepartment.Items[i].Selected = false;
            }
        }

    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (!cbSelf.Checked)
        {
            dptStartYear.Enabled = false;
            dptStartMonth.Enabled = false;
            dptDir.Enabled = false;
        }
        else
        {
            dptStartYear.Enabled = true;
            dptStartMonth.Enabled = true;
            dptDir.Enabled = true;
        }
    }
}
